package app.ui;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import database.local.Business;
import database.local.PicturesUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;

public class BitmapSyncronizer {

	PreferencesActivity context=null;
	DealsListView context_b=null;
	Bitmap bm = null;
	String page_link;
	Boolean isTypeImage=false;
	private Business unsync_b;

	public BitmapSyncronizer(PreferencesActivity cont) {
		context = cont;
	}
	
	public BitmapSyncronizer(DealsListView cont) {
		context_b = cont;
	}
	
	public void getBitmap(String url, String link) {

		// Bitmap bm = null;
		MyAsyncBitmap inst = new MyAsyncBitmap();
		page_link = link;
		try {
			inst.execute(url);
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}
	
	public void getBitmap(String url) {
		MyAsyncBitmap inst = new MyAsyncBitmap();
		try {
			inst.execute(url);
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}
	
	public void getBitmap(String url, Boolean isTypeImage, Business b) {
		this.isTypeImage = isTypeImage;
		unsync_b = b;
		MyAsyncBitmap inst = new MyAsyncBitmap();
		try {
			inst.execute(url);
		} catch (Exception e) {
			Log.e("DEBUGTAG", "Problem with sycronization", e);
		}
	}

	static class FlushedInputStream extends FilterInputStream {
		public FlushedInputStream(InputStream inputStream) {
			super(inputStream);
		}

		@Override
		public long skip(long n) throws IOException {
			long totalBytesSkipped = 0L;
			while (totalBytesSkipped < n) {
				long bytesSkipped = in.skip(n - totalBytesSkipped);
				if (bytesSkipped == 0L) {
					int b = read();
					if (b < 0) {
						break; // we reached EOF
					} else {
						bytesSkipped = 1; // we read one byte
					}
				}
				totalBytesSkipped += bytesSkipped;
			}
			return totalBytesSkipped;
		}
	}

	public class MyAsyncBitmap extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("AndroApp", "Started sync with BitmapSync");
			AndroidHttpClient httpclient = null;
			String url = params[0];
			try {
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
				conn.connect();
				InputStream is = conn.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
				bis.close();
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (httpclient != null) {
					httpclient.close();
				}
			}
			return 1;
		}


		protected void onPostExecute(Integer result) {
			if ((bm != null) && (context!=null))
			{
				context.mUserPic.setImageBitmap(bm);
			}
			else if ((bm != null) && (context_b!=null) && isTypeImage)
			{
				if ((bm.getHeight()/bm.getWidth() > 1.5) && (bm.getHeight()/bm.getWidth() < 0.35)){
					context_b.imageImage.setImageDrawable(PicturesUtil.getBusinessPicBig(unsync_b, context_b));
				}
				context_b.imageImage.setImageBitmap(bm);
			}
			else if ((bm != null) && (context_b!=null))
			{
				context_b.addImageToGrid(bm, page_link);
			}
		}

	}

}
